package com.youlu.campus.service.order;

import com.youlu.campus.common.utils.DateTimeUtils;
import com.youlu.campus.entity.CaInfo;
import com.youlu.campus.entity.OOrderInfo;
import com.youlu.campus.entity.OrderInfo;
import com.youlu.campus.entity.VO.req.ItemSoldCountReq;
import com.youlu.campus.service.item.ItemStockConfigService;
import com.youlu.campus.service.item.ItemStockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OrderItemSoldCountService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ItemStockService itemStockService;


    public void clearItemSoldCache(String activityId){
        log.info("【开始清除商品销售库存】activityId：{}",activityId);
        Query query = new Query();
        query.addCriteria(Criteria.where("activityId").is(activityId));
        List<CaInfo> caInfoList = this.mongoTemplate.find(query, CaInfo.class);
        if(CollectionUtils.isEmpty(caInfoList)){
            return;
        }
        log.warn("【开始清除商品销售库存】activityId：{} count:{}",activityId,caInfoList.size());
        caInfoList.stream().forEach(caInfo -> {
            log.info("【清除商品销售库存】activityId：{} itemId:{} level:{}",activityId,caInfo.getId(),caInfo.getLevel());
            itemStockService.deleteStock(activityId,caInfo.getId());
        });

        log.info("【结束清除商品销售库存】activityId：{}",activityId);
    }


    public void itemSoldCountInit(ItemSoldCountReq itemSoldCountReq){
        log.info("【开始商品售出】初始化");
        String activityId = itemSoldCountReq.getActivityId();
        Integer pageNo =1;
        Integer pageSize =1000;
        while (true){
            Query query = new Query();
            query.addCriteria(Criteria.where("activityId").is(activityId));
            query.addCriteria(Criteria.where("status").ne("0"));
            log.info("【订单商品售出】当前页：{}",pageNo);
            Pageable pageRequest = PageRequest.of(pageNo - 1, pageSize);
            List<OrderInfo> listOrder = this.mongoTemplate.find(query.with(pageRequest), OrderInfo.class);
            if(CollectionUtils.isEmpty(listOrder)){
                log.warn("【订单商品售出】当前页：{} 退出",pageNo);
                break;
            }
            List<String> listOrderNo = listOrder.stream().map(OrderInfo::getOrderNo).collect(Collectors.toList());
            query = new Query();
            query.addCriteria(Criteria.where("orderNo").in(listOrderNo));
            List<OOrderInfo> subOrderList = this.mongoTemplate.find(query, OOrderInfo.class);
            subOrderList.stream().forEach(subOrder->{
                itemStockService.addStock(activityId,subOrder.getItemInfoId(),1);
            });
            pageNo++;
        }
        log.info("【开始商品售出】结束");
    }
}
